package security.service;

import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import security.dao.AccountDAO;
import security.dao.CorporationDAO;
import security.dao.DashboardDAO;
import security.dao.MailDAO;
import security.dao.OrganizationDAO;
import security.dao.RoleDAO;
import security.dao.SessionDAO;
import security.dao.SystemSettingsDAO;
import security.model.Account;
import security.model.Corporation;
import security.model.Dashboard;
import security.model.Organization;
import security.model.Role;
import security.model.Session;
import security.model.SystemSettings;
import util.MyRandom;

@Stateless
public class SecurityService {

    @EJB
    SystemSettingsDAO settingsDAO;

    @EJB
    DashboardDAO dashboardDAO;

    @EJB
    MailDAO mailDAO;

    @EJB
    AccountDAO accountDAO;

    @EJB
    OrganizationDAO organizationDAO;

    @EJB
    RoleDAO roleDAO;

    @EJB
    SessionDAO sessionDAO;

    //NUEVO!
    @EJB
    CorporationDAO corporationDAO;

    public String getVersion() {
        return "1.0";
    }

	//-------------------------------------------------------------------------------
    public Session getSession(String token) {
        return sessionDAO.getByToken(token);
    }

    public void closeSession(String token) {
        Session session = getSession(token);
        session.setEnddate(new Date());
        sessionDAO.update(session);
    }

	//-------------------------------------------------------------------------------
    /**
     * @param email Email of the account
     * @param password The Account password
     */
    public String accountLogin(String username, String password) {
        String token = null;
        Account account = accountDAO.getByUsernamePassword(username, password);
        if (account != null) {
            token = MyRandom.generateUniqueToken(10);
            Session session = new Session();
            session.setAccount(account);
            session.setToken(token);
            //session.setIp(getClientIP());
            sessionDAO.save(session);
        }
        return token;
    }

    public String organizationLogin(String email, String password) {
        String token = null;
        Account account = accountDAO.getByEmailPassword(email, password);
        if (account != null) {
            token = MyRandom.generateUniqueToken(10);
            Session session = new Session();
            session.setAccount(account);
            session.setToken(token);
            //session.setIp(getClientIP());
            sessionDAO.save(session);
        }
        return token;
    }

    public String organizationRegister(Organization organization, Account account) {
        organizationDAO.save(organization);
        Role role = roleDAO.getByName("organization");
        account.setRole(role);
        account.setOrganization(organization);
        accountDAO.save(account);
        SystemSettings systemSettings = new SystemSettings();
        //systemSettings.setOrganization(organization);
        systemSettings.setLoginmode("quicklogin");
        settingsDAO.save(systemSettings);

        String text = "Hello " + account.getName() + "<br>" + "<br>"
                + "Your Access Information:" + "<br>" + "<br>"
                + "Username:" + account.getEmail() + "<br>"
                + "Password:" + account.getPassword();
        mailDAO.send(account.getEmail(), "Welcome to VirtualSkynet", text);
        return organizationLogin(account.getEmail(), account.getPassword());
    }

    public void organizationRemember(String email) {
        Account account = accountDAO.getByEmail(email);
        if (account != null) {
            //if(account.getEnabled()){
            String text = "Hello " + account.getName() + "<br>" + "<br>"
                    + "Your Access Information:" + "<br>" + "<br>"
                    + "Username:" + account.getEmail() + "<br>"
                    + "Password:" + account.getPassword();
            mailDAO.send(account.getEmail(), "Welcome to VirtualSkynet", text);
        }
    }

	//-------------------------------------------------------------------------------
    public Account getAccountById(Integer id) {
        return accountDAO.getById(id);
    }

    public Account saveAccount(Account account) {
        return accountDAO.save(account);
    }

    public Account updateAccount(Account account) {
        return accountDAO.update(account);
    }

    public void deleteAccount(Integer id) {
        accountDAO.delete(id);
    }

    public Account getAccountByEmail(String email) {
        return accountDAO.getByEmail(email);
    }

    public Account getAccountByUsername(String username) {
        return accountDAO.getByUsername(username);
    }

    public Account getAccountByOrganization(Organization organization) {
        Role role = roleDAO.getByName("organization");
        return accountDAO.getByOrganizationRole(organization, role);
    }

    public List<Account> listAccounts(Integer first, Integer max) {
        return accountDAO.list(first, max);
    }

    public List<Account> listUsersByOrganization(Organization organization, Integer first, Integer max, boolean status) {
        Role role = roleDAO.getByName("user");
        return accountDAO.listByOrganizationRole(organization, role, first, max, status);
    }

    //NUEVO!!!
    public List<Account> listAccountsByOrganization(Organization organization, Integer first, Integer max, boolean status) {
        return accountDAO.listByOrganization(organization, 0, 0);
    }

    //---------------------------------------------------------------------------------------
    public Organization getOrganizationById(Integer id) {
        return organizationDAO.getById(id);
    }

    public Organization saveOrganization(Organization organization) {
        return organizationDAO.save(organization);
    }

    public Organization updateOrganization(Organization organization) {
        return organizationDAO.update(organization);
    }

    public void deleteOrganization(Integer id) {
        organizationDAO.delete(id);
    }

    public List<Organization> listOrganizations(Integer first, Integer max) {
        return organizationDAO.list(first, max);
    }

    public Organization getOrganizationByCode(Long code) {
        return organizationDAO.getByCode(code);
    }

    public Organization getDefaultOrganization() {
        return organizationDAO.getByCode((long)1);
    }

    public Long getOrganizationTotal() {
        return organizationDAO.getTotal();
    }

    //NUEVO!!!
    public List<Organization> listOrganizationsByCorporation(Corporation corporation) {
        return organizationDAO.listByCorporation(corporation);
    }

    //NUEVO!!!
    public Organization getOrganizationByAlias(String alias) {
        return organizationDAO.getByAlias(alias);
    }

    //---------------------------------------------------------------------------------------
    public Role saveRole(Role role) {
        return roleDAO.save(role);
    }

    public Role getRoleByName(String name) {
        return roleDAO.getByName(name);
    }

	//---------------------------------------------------------------------------------------
    public SystemSettings saveSettings(SystemSettings settings) {
        return settingsDAO.save(settings);
    }

    public SystemSettings updateSettings(SystemSettings settings) {
        return settingsDAO.update(settings);
    }

    public SystemSettings getSettingsByOrganization(Organization organization) {
        return settingsDAO.getByOrganization(organization);
    }

    public Dashboard saveDashboard(Dashboard dashboard) {
        return dashboardDAO.save(dashboard);
    }

    public Dashboard updateDashboard(Dashboard dashboard) {
        return dashboardDAO.update(dashboard);
    }

    public Dashboard getDashboardById(Integer id) {
        return dashboardDAO.getById(id);
    }

    public void deleteDashboard(Integer id) {
        dashboardDAO.delete(id);
    }

    public List<Dashboard> listDashboardsByOrganization(Organization organization) {
        return dashboardDAO.listByOrganization(organization);
    }

    //****************************************************************
    // NUEVO!!!
    public Corporation getCorporationById(Integer id) {
        return corporationDAO.getById(id);
    }

    public Corporation saveCorporation(Corporation corporation) {
        return corporationDAO.save(corporation);
    }

    public Corporation updateCorporation(Corporation corporation) {
        return corporationDAO.update(corporation);
    }

    public void deleteCorporation(Integer id) {
        corporationDAO.delete(id);
    }

    public List<Corporation> listCorporations(Integer first, Integer max) {
        return corporationDAO.list(first, max);
    }

}
